package com.xuecheng.media.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.CreateUploadVideoResponse;
import com.aliyuncs.vod.model.v20170321.RefreshUploadVideoResponse;
import com.xuecheng.api.media.MediaApi;
import com.xuecheng.api.media.model.dto.MediaDTO;
import com.xuecheng.api.media.model.qo.QueryMediaModel;
import com.xuecheng.api.media.model.vo.MediaVO;
import com.xuecheng.api.media.model.vod.VodUploadRequest;
import com.xuecheng.api.media.model.vod.VodUploadToken;
import com.xuecheng.common.domain.page.PageRequestParams;
import com.xuecheng.common.domain.page.PageVO;
import com.xuecheng.common.exception.ExceptionCast;
import com.xuecheng.common.util.SecurityUtil;
import com.xuecheng.media.common.constant.MediaErrorCode;
import com.xuecheng.media.common.utils.AliyunVODUtil;
import com.xuecheng.media.convert.MediaConvert;
import com.xuecheng.media.service.MediaService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 媒资信息 前端控制器
 * </p>
 *
 * @author itcast
 */
@Slf4j
@RestController
public class MediaController implements MediaApi {

    @Autowired
    private MediaService  mediaService;

    @Value("${aliyun.region}")
    private String region;
    @Value("${aliyun.accessKeyId}")
    private String accessKeyId;
    @Value("${aliyun.accessKeySecret}")
    private String accessKeySecret;


    @PostMapping("media/vod-token")
    public VodUploadToken generateVodUploadToken(@RequestBody VodUploadRequest request) {
        try {

            // 创建vod客户端对象
            DefaultAcsClient client = AliyunVODUtil.initVodClient(region, accessKeyId, accessKeySecret);
            // 获得响应对象
            CreateUploadVideoResponse response = AliyunVODUtil.createUploadVideo(client, request.getTitle(), request.getFileName());
            // 解析响应对象并封装返回结果
            VodUploadToken vodUploadToken = null;
            vodUploadToken = new VodUploadToken();
            vodUploadToken.setUploadAuth(response.getUploadAuth());
            vodUploadToken.setUploadAddress(response.getUploadAddress());
            vodUploadToken.setVideoId(response.getVideoId());
            vodUploadToken.setRequestId(response.getRequestId());
            return vodUploadToken;

        } catch (Exception e) {
            log.error(MediaErrorCode.E_140011.getDesc() + " {}", e.getMessage());
            ExceptionCast.cast(MediaErrorCode.E_140011);
            return null;

        }
    }

    @GetMapping("media/refresh-vod-token/{videoId}")
    public VodUploadToken refreshVodUploadToken(@PathVariable String videoId) {

        try {
            // 创建vod客户端对象
            DefaultAcsClient client = AliyunVODUtil.initVodClient(region, accessKeyId, accessKeySecret);

            // 获得响应对象
            RefreshUploadVideoResponse response = AliyunVODUtil.refreshUploadVideo(client, videoId.toString());

            VodUploadToken vodUploadToken = new VodUploadToken();

            vodUploadToken.setUploadAuth(response.getUploadAuth());
            vodUploadToken.setUploadAddress(response.getUploadAddress());
            vodUploadToken.setVideoId(response.getVideoId());
            vodUploadToken.setRequestId(response.getRequestId());

            return vodUploadToken;

        } catch (Exception e) {

            log.error(MediaErrorCode.E_140015.getDesc() + " {}", e.getMessage());

            // 将系统类的异常转为业务类异常
            ExceptionCast.cast(MediaErrorCode.E_140015);
            return null;
        }

    }

    @PostMapping("media")
    public MediaDTO createMedia(@RequestBody MediaVO mediaVO) {
        Long companyId = SecurityUtil.getCompanyId();
        MediaDTO dto = MediaConvert.INSTANCE.vo2dto(mediaVO);
        dto.setCompanyId(companyId);
        MediaDTO media = mediaService.createMedia(dto);
        return media;
    }

    @GetMapping("media/preview/{mediaId}")
    public String previewMedia(@PathVariable Long mediaId) {

        Long companyId = SecurityUtil.getCompanyId();

        String previewURL = mediaService.previewMedia(mediaId, companyId);

        return previewURL;
    }


    @PostMapping("media/list")
    PageVO<MediaDTO> queryCourseList(PageRequestParams params,@RequestBody QueryMediaModel model) {

        Long companyId = SecurityUtil.getCompanyId();

        PageVO<MediaDTO> mediaDTOS = mediaService.queryCourseList(params, model, companyId);

        return mediaDTOS;
    }

//    @GetMapping("l/media/{mediaId}")
//    public RestResponse<MediaDTO> getMediaById4s(@PathVariable Long mediaId) {
//        RestResponse<MediaDTO> response = mediaService.getMediaById4s(mediaId);
//        return response;
//    }
}
